<?php

define('MIN_WAIT', 60);

function AutoLoginUser()
{
	global $mcuser;
	if(isset($_SESSION['mcuser']))
	{
		$mcuser = $_SESSION['mcuser'];
		if($mcuser->id != 0)
		{
			$mcuser = GetUserById($mcuser->id);
			$_SESSION['mcuser'] = $mcuser;
			return true;
		}
	}
	
	if($mcuser->id == 0)
	{
		if(!isset($_COOKIE['mcuser'])) return false;
		$ui = explode('+', $_COOKIE['mcuser']);
		if(count($ui) !== 3 && is_int_($ui[0]) !== true && strlen($ui[2]) !== 40) return false;
		$u = GetUserById($ui[0]);
		if($u === false) return false;
		if($u->valid !== 0 && $u->name !== $ui[1] && $u->password !== $ui[2]) return false;
		$mcuser = $u;
		$_SESSION['mcuser'] = $mcuser;
		LogLogin($mcuser, true);
		return true;
	}
	return false;
}

function LoginUser($name, $pass)
{
	global $mcuser;
	$ui = GetUser($name);
	if($ui === false) return 'Cet utilisateur n\'existe pas';
	if($ui->valid != 0) return 'Compte non activé';
	if($pass !== $ui->password) return 'Mot de passe incorrect';
	$_SESSION['mcuser'] = $ui;
	$mcuser = $ui;
	$expire = time()+(3600*24*7);
	pun_setcookie($mcuser->id, $mcuser->password, $expire);
	setcookie('mcuser', $mcuser->id . '+' . $mcuser->name . '+' . $mcuser->password, $expire, '/');
	LogLogin($mcuser, false);
	return true;
}

function AutoLoginUserContest()
{
	global $mcuser;
	if(isset($_SESSION['mcuser']) && isset($_SESSION['mctime']))
	{
		$mcuser = $_SESSION['mcuser'];
		if($mcuser->id != 0)
		{
			$mcuser = GetUserById($mcuser->id);
			$_SESSION['mcuser'] = $mcuser;
			return true;
		}
	}
	return false;
}

function LoginUserContest($name, $pass)
{
	global $mcuser;
	if(defined('MC_CONTEST') == false) return false;
	if(isset($_SESSION['mcuser']) && $_SESSION['mcuser']->id != 0) return false;
	$loginerror = 0;
	$ui = GetUser($name);
	if($ui === false) return 'Cet utilisateur n\'existe pas';
	if($ui->valid != 0) return 'Votre compte n\'est pas encore activé';
	if($pass !== $ui->password) return 'Mot de passe incorrect';
	if(CheckBruteForce($ui)) return 'BruteForce interdit';
	$_SESSION['mcuser'] = $ui;
	$_SESSION['mctime'] = time();
	SetUserLastTry($ui->id, $_SESSION['mctime']);
	$mcuser = $ui;
	return true;
}

function LogoutUser()
{
	global $db;
	global $pun_user;
	global $mcuser;
	global $defaultuser;
	$db->query('DELETE FROM ' . $db->prefix . 'online WHERE user_id=' . $pun_user['id']);// or error(‘Unable to delete from online list’, __FILE__, __LINE__, $db->error());
	if(isset($pun_user['logged']))
	{
		$db->query('UPDATE ' . $db->prefix . 'users SET last_visit=' . $pun_user['logged'] . ' WHERE id=' . $pun_user['id']);// or error(‘Unable to update user visit data’, __FILE__, __LINE__, $db->error());
	}
	pun_setcookie(1, pun_hash(uniqid(rand(), true)), time() + 31536000);
	setcookie('mcuser', '', time() - 3600, '/');
	set_default_user();
	unset($_SESSION['mcuser']);
	session_destroy();
	session_start();
	$mcuser = $defaultuser;
	$_SESSION['mcuser'] = $mcuser;
}

function CheckBruteForce($u)
{
	if(time() - $u->lasttry < MIN_WAIT)
	{
		$c = GetContestById(MC_CONTEST);
		LogBruteForce($u, $c);
		return true;
	}
	return false;
}

function is_int_($int)
{
	if(is_numeric($int) === true)
	{
		if((int)$int == $int) return true;
		else return false;
	}
	return false;
}

?>